import datetime
from typing import List
from package.fastapi.schema import Page, BatchId
from pydantic import Field, BaseModel


class Email(BaseModel):
    server: str = Field(..., title='邮件服务器地址')
    port: int = Field(25, title='端口')
    is_ssl: bool = Field(False, title='是否走加密通道')
    sender: str = Field('', title='邮件发件人')
    sender_alias: str = Field('', title='邮件发件人显示名')
    password: str = Field('', title='邮件发件人密码')
    receiver: List[str] = Field(..., title='收件人地址')


class DingTalk(BaseModel):
    """钉钉配置"""
    webhook_url: str = Field(..., title='回调链接')
    app_secret: str = Field(..., title='密钥')
    to_user: List[str] = Field([], title='接收人列表')
    to_all: bool = Field(False, title='所有人')


class WeChat(BaseModel):
    """微信"""
    corp_id: str = Field('', title='企业ID')
    secret_key: str = Field('', title='密钥')
    agent_id: str = Field('', title='发送ID')
    to_user: List[str] = Field([], title='接收人列表')
    to_party: List[str] = Field([], title='接收部门列表')


class Script(BaseModel):
    """脚本"""
    script: str = Field('', title='脚本')
    script_type: str = Field('', title='脚本类型 Python|Shell|Perl')
    variables: dict = Field({}, title='参数')


class Notify(BaseModel):
    name: str = Field(..., title='通道名')
    description: str = Field('', title='通道描述')

    class Config:
        orm_mode = True


class NotifyOut(Notify):
    id: str = Field(..., title='ID')
    type: str = Field(..., title='通道类型')
    created_at: datetime.datetime = Field(None, title='')

    class Config:
        orm_mode = True
        json_encoders = {datetime.datetime: lambda dt: int(dt.timestamp() * 1000)}


class EmailNotifyIn(Notify):
    """邮件"""
    configure: Email = Field(..., title='')


class WeChatNotifyIn(Notify):
    """邮件"""
    configure: WeChat = Field(..., title='')


class DingTalkNotifyIn(Notify):
    """邮件"""
    configure: DingTalk = Field(..., title='')


class ScriptNotifyIn(Notify):
    """邮件"""
    configure: Script = Field(..., title='')


class EmailNotifyEdit(BaseModel):
    description: str = Field(None, title='通道描述')
    configure: Email = Field(None, title='通道配置')


class WeChatNotifyEdit(BaseModel):
    description: str = Field(None, title='通道描述')
    configure: WeChat = Field(None, title='通道配置')


class DingTalkNotifyEdit(BaseModel):
    description: str = Field(None, title='通道描述')
    configure: DingTalk = Field(None, title='通道配置')


class ScriptNotifyEdit(BaseModel):
    description: str = Field(None, title='通道描述')
    configure: Script = Field(None, title='通道配置')


class EmailNotifyOut(EmailNotifyIn, NotifyOut):
    pass


class WeChatNotifyOut(WeChatNotifyIn, NotifyOut):
    pass


class DingTalkNotifyOut(DingTalkNotifyIn, NotifyOut):
    pass


class ScriptNotifyOut(ScriptNotifyIn, NotifyOut):
    pass


class PageEmail(Page):
    data: List[EmailNotifyOut] = Field(..., title='邮件列表')


class PageWeChat(Page):
    data: List[WeChatNotifyOut] = Field(..., title='微信列表')


class PageDingTalk(Page):
    data: List[DingTalkNotifyOut] = Field(..., title='钉钉列表')


class PageScript(Page):
    data: List[ScriptNotifyOut] = Field(..., title='脚本列表')


class EmailVerify(BaseModel):
    """邮件"""
    name: str = Field(..., title='通道名称')
    configure: Email = Field(..., title='')


class WeChatVerify(BaseModel):
    name: str = Field(..., title='通道名称')
    configure: WeChat = Field(..., title='')


class DingTalkVerify(BaseModel):
    name: str = Field(..., title='通道名称')
    configure: DingTalk = Field(..., title='')


class ScriptVerify(BaseModel):
    name: str = Field(..., title='通道名称')
    configure: Script = Field(..., title='')


#  通告消息格式
class Message(BaseModel):
    title: str = Field('', title='消息标题')
    message: str = Field('', title='消息内容')
    url: str = Field('', title='消息地址')

    class Config:
        extra = 'allow'


class BatchNotifyId(BatchId):
    pass
